import request from "@/utils/request";

import { Content, GiteeInfo, Pagination, User } from "@/views/type";

interface ApiRequest {
  // 根据实际请求参数结构定义
  [key: string]: any; // 临时使用any，具体根据实际情况定义
}

enum API {
  BookList_URL = "/ebook/list",
  BookSave_URL = "/ebook/save",
  BookDelete_URL = "/ebook/delete/", // 修正拼写错误
  CategoryAll_URL = "/category/all", // 修正枚举名
  CategoryList_URL = "/category/list",
  CategorySave_URL = "/category/save",
  CategoryDelete_URL = "/category/delete/",
  DocAll_URL = "/doc/all", // 修正枚举名
  SelectDocAll_URL = "/doc/all/",
  DocfindContent = "/doc/find-content/",
  DocList_URL = "/doc/list",
  DocSave_URL = "/doc/save",
  DocDelete_URL = "/doc/delete/",
  DocVote_URL = "/doc/vote/",
  UserList_URL = "/user/list",
  UserSave_URL = "/user/save",
  UserDelete_URL = "/user/delete/",
  UserLogin_URL = "/user/login",
  UserLogout_URL = "/user/logout/",
  UserInfo_URL = "/user/info",
  UseResetPasswd_URL = "/user/reset-password",
  GiteeLogin_URL = "/user/giteelogin/",
  GetStatistic = "/ebook-snapshot/get-statistic",
  Get30statistic = "/ebook-snapshot/get-30-statistic",
}

export function useApi() {
  //  function login(params: ApiRequest) {
  //   // 参数校验（示例，根据实际需要调整）
  //   if (!params.username || !params.password) {
  //     throw new Error("用户名和密码不能为空");
  //   }

  //     const response =  request<>({
  //       url: "/users/login",
  //       method: "post",
  //       data: params,
  //     });
  //     return response.data;

  // }

  function booklist<T>(params?: ApiRequest) {
    // 注意：这里的params进行了类型限制，实际使用时需根据接口调整
    return request<T>({
      url: API.BookList_URL,
      method: "get",
      data: params,
    });
  }

  function saveBook(params: ApiRequest) {
    // 更改函数名为saveBook以符合命名规范
    return request({
      url: API.BookSave_URL,
      method: "post",
      data: params,
    });
  }

  function deleteBook(id: number, params?: ApiRequest) {
    // 添加参数校验来避免安全风险
    if (typeof id !== "number" || id <= 0) {
      throw new Error("无效的书籍ID");
    }
    return request<Content<null>>({
      url: API.BookDelete_URL + id,
      method: "delete",
      data: params,
    });
  }

  function getCategoryAll<T>() {
    // 函数名调整为getCategoryAll以更明确其作用
    return request<T>({
      url: API.CategoryAll_URL,
      method: "get",
    });
  }

  function getCategoryList<T>(params?: ApiRequest) {
    // 注意：这里的params进行了类型限制，实际使用时需根据接口调整
    return request<T>({
      url: API.CategoryList_URL,
      method: "get",
      data: params,
    });
  }
  function saveCategoryList<T>(params?: ApiRequest) {
    // 注意：这里的params进行了类型限制，实际使用时需根据接口调整
    return request<T>({
      url: API.CategorySave_URL,
      method: "post",
      data: params,
    });
  }
  function deleteCategory<T>(id: number, params?: ApiRequest) {
    // 添加参数校验来避免安全风险
    if (typeof id !== "number" || id <= 0) {
      throw new Error("无效的书籍ID");
    }
    return request<T>({
      url: API.CategoryDelete_URL + id,
      method: "delete",
      data: params,
    });
  }

  function getDocAll<T>() {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.DocAll_URL,
      method: "get",
    });
  }
  function getSelectDocAll<T>(id: number) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.SelectDocAll_URL + id,
      method: "get",
    });
  }
  function getDocContent(id: number) {
    // 函数名调整为getDocAll以更明确其作用
    return request<string>({
      url: API.DocfindContent + id,
      method: "get",
    });
  }

  //  function getDocList(params?: ApiRequest) {
  //   // 注意：这里的params进行了类型限制，实际使用时需根据接口调整
  //   return  request({
  //     url: API.DocList_URL,
  //     method: "get",
  //     data: params,
  //   });
  // }
  function saveDocList(params?: ApiRequest) {
    // 注意：这里的params进行了类型限制，实际使用时需根据接口调整
    return request({
      url: API.DocSave_URL,
      method: "post",
      data: params,
    });
  }
  function deleteDoc(deleteIds: Array<string>, params?: ApiRequest) {
    // 添加参数校验来避免安全风险

    return request({
      url: API.DocDelete_URL + deleteIds.join(","),
      method: "delete",
      data: params,
    });
  }
  function DocVote_URL<T>(id: number) {
    return request<T>({
      url: API.DocVote_URL + id,
      method: "get",
    });
  }

  // -------user------
  function getUserList<T>(params: Pagination) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UserList_URL,
      method: "get",
      data: params,
    });
  }

  function SaveUser<T>(user: User) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UserSave_URL,
      method: "post",
      data: user,
    });
  }
  function deleteUser<T>(id: number) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UserDelete_URL + id,
      method: "delete",
    });
  }
  function ResetpassUser<T>(user: User) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UseResetPasswd_URL,
      method: "post",
      data: user,
    });
  }
  function loginUser<T>(user: User) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UserLogin_URL,
      method: "post",
      data: user,
    });
  }
  function logoutUser<T>(id: string) {
    // 函数名调整为getDocAll以更明确其作用
    return request<T>({
      url: API.UserLogout_URL + id,
      method: "get",
    });
  }
  function GiteeCodeToSeivice(code: any) {
    return request<GiteeInfo>({
      url: API.GiteeLogin_URL,
      method: "post",
      data: {data:code},
    });
  }

  function getStatistic<T>() {
    return request<T>({
      url: API.GetStatistic,
      method: "get",
    });
  }
  function get30Statistic<T>() {
    return request<T>({
      url: API.Get30statistic,
      method: "get",
    });
  }

  return {
    booklist,
    saveBook,
    deleteBook,
    getCategoryAll,
    getCategoryList,
    saveCategoryList,
    deleteCategory,
    getDocAll,
    getSelectDocAll,
    getDocContent,
    saveDocList,
    deleteDoc,
    DocVote_URL,
    getUserList,
    SaveUser,
    deleteUser,
    ResetpassUser,
    loginUser,
    logoutUser,
    GiteeCodeToSeivice,
    getStatistic,
    get30Statistic
  };
}
